package com.idak.spring.web.flow.valodator;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.idak.spring.web.flow.SearchCriteria;

@Component
public class SearchCriteriaValidator implements Validator {

	@Override
	public boolean supports(Class<?> aClass) {
		return SearchCriteria.class.equals(aClass);
	}

	@Override
	public void validate(Object input, Errors errors) {
		if(input instanceof SearchCriteria){
			SearchCriteria searchCriteria = (SearchCriteria) input;
			if(!StringUtils.hasText(searchCriteria.getFirstName()) && !StringUtils.hasText(searchCriteria.getLastName())){
				errors.rejectValue("firstName", "search.criteria.form.error");
			}
		}
	}

}
